var Datatable = function () {

  var tableOptions; // main options
  var dataTable; // datatable object
  var table; // actual table jquery object
  var tableContainer; // actual table container object
  var tableWrapper; // actual table wrapper jquery object
  var tableInitialized = false;
  var the;

  var countSelectedRecords = function () {
    var selected = $('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).size();
    var text = tableOptions.dataTable.language.metronicGroupActions;
    if (selected > 0) {
      $('.table-group-actions > span', tableWrapper).text(text.replace("_TOTAL_", selected));
    } else {
      $('.table-group-actions > span', tableWrapper).text("");
    }
  };

  var reloadTable = function (pageFlag) {
    dataTable.draw(pageFlag)
  };

  return {

    //main function to initiate the module
    init: function (options) {

      if (!$().dataTable) {
        return;
      }

      the = this;
      // default settings
      options = $.extend(true, {
        src: "", // actual table
        filterApplyAction: "filter",
        filterCancelAction: "filter_cancel",
        loadingMessage: '载入中...',
        onQuery: function (data) {
        },
        onSortColumn: function (sortColumn, sortDirection) {
          return customGlobal.onSortColumnDefault(sortColumn, sortDirection);
        },
        dataTable: {
          "dom": "<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'<'table-group-actions pull-right'>>r><'table-scrollable't><'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'>>", // datatable layout
          "lengthMenu": [5, 10, 25, 50, 75, 100],
          search: true,
          "pageLength": 10, // default records per page
          "language": { // language settings
            // metronic spesific
            "metronicGroupActions": " _TOTAL_ " + i18n["dataTables.recordsSelected"],
            "metronicAjaxRequestGeneralError": i18n["dataTables.badRequest"],

            // data tables spesific
            "lengthMenu": "<span class='seperator'>|</span>" + i18n["dataTables.view"] + " _MENU_ " + i18n["dataTables.records"],
            "info": "<span class='seperator'>|</span>" + i18n["dataTables.total"] + " _TOTAL_ " + i18n["dataTables.records"],
            "infoEmpty": i18n["dataTables.noRecords"],
            "emptyTable": i18n["dataTables.noData"],
            "zeroRecords": i18n["dataTables.noMatch"],
            "paginate": {
              "previous": i18n['dataTables.prev'],
              "next": i18n['dataTables.next'],
              "last": i18n['dataTables.last'],
              "first": i18n['dataTables.first'],
              "page": i18n['dataTables.page'],
              "pageOf": i18n['dataTables.of']
            }
          },
          "columnDefs": [{  // set default column settings
            'orderable': false,
            'targets': [0]
          }, {
            "searchable": false,
            "targets": [0]
          }],
          "orderCellsTop": true,
          "pagingType": "bootstrap_extended", // pagination type(bootstrap, bootstrap_full_number or bootstrap_extended)
          "autoWidth": true, // disable fixed width and enable fluid table
          "processing": false, // enable/disable display message box on record load
          "serverSide": true, // enable/disable server side ajax loading
          "ajax": { // define ajax settings
            "url": "", // ajax URL
            "type": "POST", // request type
            "timeout": 20000,
            "data": function (data) { // add request parameters before submit
              if (options.onQuery) {
                options.onQuery(data);
              }
              if (options.onSortColumn) {
                if (data.order[0]) {
                  var sortTemp = options.onSortColumn(options.dataTable.columns[data.order[0].column].data, data.order[0].dir);
                  data.sortField = sortTemp.sortColumn;
                  data.sortOrder = sortTemp.sortDirection;
                } else {
                  data.sortField = "";
                  data.sortOrder = "";
                }
              }
              data.pageNo = parseInt(data.start / data.length, 10) + 1;
              data.pageSize = data.length;
              Metronic.blockUI({
                message: tableOptions.loadingMessage,
                target: tableContainer,
                overlayColor: 'none',
                cenrerY: true,
                boxed: true
              });
            },
            "dataSrc": function (res) { // Manipulate the data returned from the server
              if ($('.group-checkable', table).size() === 1) {
                $('.group-checkable', table).attr("checked", false);
                $.uniform.update($('.group-checkable', table));
              }

              if (tableOptions.onSuccess) {
                tableOptions.onSuccess.call(undefined, the);
              }

              Metronic.unblockUI(tableContainer);

              return res.data;
            },
            "error": function () { // handle general connection errors
              if (tableOptions.onError) {
                tableOptions.onError.call(undefined, the);
              }

              Metronic.alert({
                type: 'danger',
                icon: 'warning',
                message: tableOptions.dataTable.language.metronicAjaxRequestGeneralError,
                container: tableWrapper,
                place: 'prepend'
              });

              Metronic.unblockUI(tableContainer);
            }
          },

          "drawCallback": function (oSettings) { // run some code on table redraw
            if (tableInitialized === false) { // check if table has been initialized
              tableInitialized = true; // set table initialized
              table.show(); // display table
            }
            Metronic.initUniform($('input[type="checkbox"]', table)); // reinitialize uniform checkboxes on each table reload
            countSelectedRecords(); // reset selected records indicator
          }
        }
      }, options);

      tableOptions = options;

      // create table's jquery object
      table = $(options.src);
      tableContainer = table.parents(".table-container");

      // apply the special class that used to restyle the default datatable
      var tmp = $.fn.dataTableExt.oStdClasses;

      $.fn.dataTableExt.oStdClasses.sWrapper = $.fn.dataTableExt.oStdClasses.sWrapper + " dataTables_extended_wrapper";
      $.fn.dataTableExt.oStdClasses.sFilterInput = "form-control input-small input-sm input-inline";
      $.fn.dataTableExt.oStdClasses.sLengthSelect = "form-control input-xsmall input-sm input-inline";

      // initialize a datatable
      dataTable = table.DataTable(options.dataTable);

      // revert back to default
      $.fn.dataTableExt.oStdClasses.sWrapper = tmp.sWrapper;
      $.fn.dataTableExt.oStdClasses.sFilterInput = tmp.sFilterInput;
      $.fn.dataTableExt.oStdClasses.sLengthSelect = tmp.sLengthSelect;

      // get table wrapper
      tableWrapper = table.parents('.dataTables_wrapper');

      // build table group actions panel
      if ($('.table-actions-wrapper', tableContainer).size() === 1) {
        $('.table-group-actions', tableWrapper).html($('.table-actions-wrapper', tableContainer).html()); // place the panel inside the wrapper
        $('.table-actions-wrapper', tableContainer).remove(); // remove the template container
      }
      // handle group checkboxes check/uncheck
      $('.group-checkable', table).change(function () {
        var set = $('tbody > tr > td:nth-child(1) input[type="checkbox"]', table);
        var checked = $(this).is(":checked");
        $(set).each(function () {
          $(this).attr("checked", checked);
        });
        $.uniform.update(set);
        countSelectedRecords();
      });

      // handle row's checkbox click
      table.on('change', 'tbody > tr > td:nth-child(1) input[type="checkbox"]', function () {
        countSelectedRecords();
      });

      // handle filter submit button click
      table.on('click', '.filter-submit', function (e) {
        e.preventDefault();
        dataTable.ajax.reload();
      });

      // handle filter cancel button click
      table.on('click', '.filter-cancel', function (e) {
        e.preventDefault();
        $('textarea.form-filter, select.form-filter, input.form-filter', table).each(function () {
          $(this).val("");
        });
        $('input.form-filter[type="checkbox"]', table).each(function () {
          $(this).attr("checked", false);
        });
        dataTable.ajax.reload();
      });

      var tableId = tableOptions.src.attr("id");
      //显示列选择按钮事件
      $('#' + tableId + 'ToggleColumn').find('input[type="checkbox"]').on("change", function () {
        var iCol = parseInt($(this).attr("data-column"));
        dataTable.column(iCol).visible(!dataTable.column(iCol).visible());
      });

      $("#" + tableId + "Reload").on("click", function () {
        reloadTable(false);
      })
    },

    getSelectedRowsCount: function () {
      return $('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).size();
    },

    getSelectedRows: function () {
      var rows = [];
      $('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).each(function () {
        rows.push($(this).val());
      });

      return rows;
    },

    getDataTable: function () {
      return dataTable;
    },

    getTableWrapper: function () {
      return tableWrapper;
    },

    gettableContainer: function () {
      return tableContainer;
    },

    getTable: function () {
      return table;
    },

    /**
     * 验证中文的方法
     * @param target 输入框的内容
     * @param message 提示内容
     * @returns {boolean}
     */
    validateZHCode: function (target, message) {
      if (target == "")return true;
      //alert(target);
      if (/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(target)) {
        toast.error(message);
        return true;
      } else {
        return false;
      }
    },

    /**
     * 验证form中的input是中文方法
     * @param target 输入框 所在form的id
     * @param message 提示内容
     * @returns {boolean}
     */
    validateAllZHCode: function (target, message) {
      target = "#" + target;
      var flag = false;
      $(target).find("input,textarea").each(function () {
        if (/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(this.value)) {
          toast.error(message);
          flag = true;
          return false;
        }
      });
      return flag;
    },

    /**
     * 验证最大长度字符
     * @param target 输入框的id
     * @param max 最大长度
     * @param text 输入框的名称
     * @returns {boolean}
     */
    validateMaxLength: function (target, max, text) {
      target = "#" + target;
      var message = text + i18n['tooLong'] + " " + max + " " + i18n['tooLongEnd'];
      if ($(target).val().length > max) {
        toast.error(message);
        $(target).focus();
        return true;
      } else {
        return false;
      }
    },

    /**
     * 验证非空
     * @param target 输入框的id
     * @param text 输入框的名称
     */
    validateNull: function (target, text) {
      target = "#" + target;
      var message = text + " " + i18n['canNotEmp'];
      if ($(target).val() == "" || $(target).val() == null) {
        toast.error(message);
        $(target).focus();
        return true;
      } else {
        return false;
      }
    },

    /**
     * 验证form中所有input是否非空
     * @param target 输入框 所在form的id
     */
    validateNullAll: function (target) {
      target = "#" + target;
      var flag = false;
      $(target).find("input").each(function () {
        if ($(this).val() == "" || $(this).val() == null) {
          var text = $(this).attr("placeholder");
          var message = text + " " + i18n['canNotEmp'];
          toast.error(message);
          this.focus();
          flag = true;
          return false;
        }
      });
      return flag;
    },

    /**
     * 验证数字(+/-)123(.1231)
     * @param target 输入框的id
     */
    validateDigits: function (target) {
      target = "#" + target;
      if (!/^-?[0-9]+(.[0-9]+)?$/.test($(target).val())) {
        var message = $(target).attr("placeholder") + " " + i18n['notDigits'];
        toast.error(message);
        $(target).focus();
        return true;
      } else {
        return false;
      }
    },

    /**
     * 验证正整数123
     * @param target 输入框的id
     */
    validatePositiveNumber: function (target) {
      target = "#" + target;
      if (!/^[0-9]+$/.test($(target).val())) {
        var message = $(target).attr("placeholder") + " " + i18n['notPositiveNumber'];
        toast.error(message);
        $(target).focus();
        return true;
      } else {
        return false;
      }
    },

    /**
     * 验证数字 5位正小数
     * @param target 输入框的id
     */
    validateNumber: function (target) {
      target = "#" + target;
      if (!/^[0-9]+(.[0-9]{0,5})?$/.test($(target).val())) {
        var message = $(target).attr("placeholder") + " " + i18n['notNumber5'];
        toast.error(message);
        $(target).focus();
        return true;
      } else {
        return false;
      }
    },

    /**
     * 验证数字 2位正小数
     * @param target 输入框的id
     */
    validateNumber2: function (target) {
      target = "#" + target;
      if (!/^[0-9]+(.[0-9]{0,2})?$/.test($(target).val())) {
        var message = $(target).attr("placeholder") + " " + i18n['notNumber2'];
        toast.error(message);
        $(target).focus();
        return true;
      } else {
        return false;
      }
    },
    /**
     * 验证重复输入密码
     * @param target 第二次输入框的id
     * @param comparison 第一次输入框的id
     */
    validatePasswordRepeat: function (target, comparison) {
      target = "#" + target;
      comparison = "#" + comparison;
      if ($(target).val() != $(comparison).val()) {
        var message = i18n['diffInput'];
        toast.error(message);
        $(target).focus();
        return true;
      } else {
        return false;
      }
    },
    /**
     * 验证google框
     * @param target 输入框的id
     */
    validateGoogle: function (target) {
      target = "#" + target;
      if ($(target).getGAutoHiddenValue() == "") {
        var message = $(target).attr("placeholder") + " " + i18n['notInGoogle'];
        toast.error(message);
        $(target).focus();
        return true;
      } else {
        return false;
      }
    },

    /**
     * 验证form中所有google框
     * @param target 输入框 所在form的id
     */
    validateGoogleAll: function (target) {
      target = "#" + target;
      var flag = false;
      $(target).find("input").each(function () {
        var text = $(this).attr("placeholder");
        if ($(this).getGAutoHiddenValue() == "" && text != "") {
          var message = text + " " + i18n['notInGoogle'];
          toast.error(message);
          this.focus();
          flag = true;
          return false;
        }

      });
      return flag;
    },

    reloadTable: reloadTable
  };

};
